package com.tjsoft.system.web.account;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.tjsoft.system.entity.base.Comp;
import com.tjsoft.system.service.AccountManager;
import com.tjsoft.system.service.ImportManager;
import com.tjsoft.system.web.CrudActionSupport;

//namespace与application下的目录对应dispatcher
@Namespace("/app/sys/comp")
@Results( { @Result(name = "reload", location = "/app/sys/comp/comp!list", type = "redirect")})
public class CompAction extends CrudActionSupport<Comp> {

	private static final long serialVersionUID = 8683878162525847072L;

	@Autowired
	private AccountManager accountManager;
	@Autowired
	private ImportManager importManager;

	private Long id ;
	private Comp entity;
	private Page<Comp> page = new Page<Comp>(10);
	private Long[] chkDel; 
	private String errMsg = "";
	
	public void validateSave(){
		try {
			if("".equals(entity.getName())){
				this.addFieldError("msg.name", "姓名不能为空");
			}
			if("".equals(entity.getCompName())){
				this.addFieldError("msg.compName", "企业名称不能为空");
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	@Override
	public String delete()  {
		try {
			if(chkDel!=null && chkDel.length>0){
				for(Long id:chkDel){
					accountManager.deleteComp(id);
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除微企记录失败");
		}
		return RELOAD;
	}
	@Override
	public String input()  {
		return INPUT;
	}
	
	@Override
	public String list() {
		try {
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
			page = accountManager.getCompDAO().findPage(page, filters);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("加载微企记录失败");
		}
		return "list";
	}
	
	public String list4select() {
		try {
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
			page = accountManager.getCompDAO().findPage(page, filters);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("加载微企记录失败");
		}
		return "list4select";
	}
	
	public void list4test() {
		try {
			System.out.println(1);
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
			page = accountManager.getCompDAO().findPage(page, filters);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("加载微企记录失败");
		}
	}
	
	@SuppressWarnings("unchecked")
	public void getCompInfo(){
		PrintWriter out = null;
		try {
			Comp comp = accountManager.getCompDAO().get(id);
			Struts2Utils.getResponse().setCharacterEncoding("utf-8");
			out = Struts2Utils.getResponse().getWriter();
			
			SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
			JSONObject obj = new JSONObject();
			obj.put("compName", comp.getCompName());
			obj.put("name", comp.getName());
			obj.put("address", comp.getAddress());
			obj.put("phone", comp.getPhone());
			obj.put("bankAddress", comp.getBankAddress());
			obj.put("bankNo", comp.getBankNo());
			obj.put("industry", comp.getIndustry());
			obj.put("empCount", comp.getEmpCount());
			if(comp.getChengliDate()==null || "".equals(comp.getChengliDate())){
				obj.put("chengliDate", "");
			}else{
				obj.put("chengliDate", sdf.format( comp.getChengliDate()));
			}
			obj.put("zhuceMoney", comp.getZhuceMoney());
			obj.put("selfTouzhiMoney", comp.getSelfTouzhiMoney());
			obj.put("govBuzhuMoney", comp.getGovBuzhuMoney());
			
			out.print(obj.toString());
			out.flush();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		} finally{
			out.close();
		}
	}
	
	public void impt(){
		String path = ServletActionContext.getServletContext().getRealPath("/");
		try {
			importManager.importInitData(path);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
	}
	
	@Override
	protected void prepareModel()  {
		if (id != null) {
			try {
				entity = accountManager.getCompDAO().get(id);
			} catch (Exception e) {
				logger.error(e.getMessage(),e);
				addActionMessage("加载微企记录失败");
			}
		} else {
			entity = new Comp();
		}
	}
	@Override
	public String save()  {
		try {
			accountManager.saveComp(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("保存微企记录失败");
		}
		return RELOAD;
	}
	
	public Comp getModel() {
		return entity;
	}
	
	//＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝
	public AccountManager getAccountManager() {
		return accountManager;
	}
	
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
	public Page<Comp> getPage() {
		return page;
	}
	public void setPage(Page<Comp> page) {
		this.page = page;
	}
	public Comp getEntity() {
		return entity;
	}
	public void setEntity(Comp entity) {
		this.entity = entity;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	public String getErrMsg() {
		return errMsg;
	}

	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	public Long[] getChkDel() {
		return chkDel;
	}

	public void setChkDel(Long[] chkDel) {
		this.chkDel = chkDel;
	}

	public ImportManager getImportManager() {
		return importManager;
	}

	public void setImportManager(ImportManager importManager) {
		this.importManager = importManager;
	}
}
